package com.qnbc.mystery.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qnbc.mystery.mapper.CollectMapper;
import com.qnbc.mystery.mapper.OrderDetailMapper;
import com.qnbc.mystery.pojo.domain.Collect;
import com.qnbc.mystery.pojo.domain.FoodOrder;
import com.qnbc.mystery.pojo.domain.OrderDetail;
import com.qnbc.mystery.pojo.domain.User;
import com.qnbc.mystery.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.qnbc.mystery.mapper.FoodOrderMapper;
import com.qnbc.mystery.service.IFoodOrderService;

/**
 * 订单管理Service业务层处理
 *
 * @author guzj
 * @date 2024-01-24
 */
@Service
public class FoodOrderServiceImpl extends ServiceImpl<FoodOrderMapper, FoodOrder> implements IFoodOrderService {
    @Autowired
    private FoodOrderMapper foodOrderMapper;

    @Autowired
    private OrderDetailMapper orderDetailMapper;

    /**
     * 查询订单管理
     *
     * @param id 订单管理主键
     * @return 订单管理
     */
    @Override
    public FoodOrder selectFoodOrderById(Long id) {
        FoodOrder foodOrder = foodOrderMapper.selectFoodOrderById(id);
        OrderDetail orderDetail = new OrderDetail();
        orderDetail.setMid(id);
        List<OrderDetail> orderDetails = orderDetailMapper.selectOrderDetailList(orderDetail);

        foodOrder.setGoods(orderDetails);
        return foodOrder;
    }

    /**
     * 查询订单管理列表
     *
     * @param foodOrder 订单管理
     * @return 订单管理
     */
    @Override
    public List<FoodOrder> selectFoodOrderList(FoodOrder foodOrder) {

        return foodOrderMapper.selectFoodOrderList(foodOrder);
    }

    /**
     * 新增订单管理
     *
     * @param foodOrder 订单管理
     * @return 结果
     */
    @Override
    public int insertFoodOrder(FoodOrder foodOrder) {
        //获取商品信息，购买数量，选择相关规格
        foodOrder.setCreatedTime(new Date());
        foodOrder.setUpdatedTime(new Date());
        User currentUser = TokenUtils.getCurrentUser();
        foodOrder.setCreatedBy(currentUser.getUsername());
        int i = foodOrderMapper.insertFoodOrder(foodOrder);
        if (i > 0) {
            //新增订单详情
            OrderDetail orderDetail = new OrderDetail();
            orderDetailMapper.insertOrderDetail(orderDetail);
        }
        return i;
    }

    /**
     * 修改订单管理
     *
     * @param foodOrder 订单管理
     * @return 结果
     */
    @Override
    public int updateFoodOrder(FoodOrder foodOrder) {

        return foodOrderMapper.updateFoodOrder(foodOrder);
    }

    /**
     * 批量删除订单管理
     *
     * @param ids 需要删除的订单管理主键
     * @return 结果
     */
    @Override
    public int deleteFoodOrderByIds(Long[] ids) {
        return foodOrderMapper.deleteFoodOrderByIds(ids);
    }

    /**
     * 删除订单管理信息
     *
     * @param id 订单管理主键
     * @return 结果
     */
    @Override
    public int deleteFoodOrderById(Long id) {
        return foodOrderMapper.deleteFoodOrderById(id);
    }

    @Override
    public FoodOrder selectOrderById(Long aLong) {
        return null;
    }
}
